<template>
  <div style="margin-left: 20px">
    <div>
      <div class="head"><p>新增获奖</p></div>
    </div>

    <div style="margin-top: 30px">
      <div class="head_2"><p>[基本信息]</p></div>
      <div>
        <el-form :model="form" label-width="120px" :rules="rules" ref="form">
          <el-form-item label="奖励名称" prop="awardName">
            <el-input v-model="form.awardName" style="width: 50%" placeholder="请输入奖励名称" ></el-input>
          </el-form-item>
          <el-form-item label="成果名称" prop="resultName">
            <el-input v-model="form.resultName" style="width: 50%" placeholder="请输入成果名称"></el-input>
          </el-form-item>
          <el-form-item label="所属单位" prop="affiliatedUnit">
            <el-input v-model="form.affiliatedUnit" style="width: 50%" placeholder="请输入所属单位"></el-input>
          </el-form-item>

          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="获奖人数" >
                <el-input v-model="form.winnersNumber" style="width: 200px" placeholder="请输入获奖人数"></el-input>
              </el-form-item>
              <el-form-item label="获奖日期" prop="awardDate">
                <el-date-picker clearable
                                v-model="form.awardDate"
                                type="date"
                                value-format="yyyy-MM-dd"
                                placeholder="请选择获奖日期">
                </el-date-picker>
              </el-form-item>
              <el-form-item label="获奖等级" prop="awardGrade">
                <el-select v-model="form.awardGrade" placeholder="请选择获奖等级">
                  <el-option
                    v-for="dict in dict.type.work_award_grade"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="完成单位">
                <el-input v-model="form.completionUnit" style="width: 300px" placeholder="请输入完成单位"></el-input>
              </el-form-item>
              <el-form-item label="总参加单位数">
                <el-input v-model="form.participationgUnitsNum" style="width: 300px" placeholder="请输入总参加单位数"></el-input>
              </el-form-item>
              <el-form-item label="奖励批准号">
                <el-input v-model="form.awardCode" style="width: 300px" placeholder="请输入奖励批准号"></el-input>
              </el-form-item>
              <el-form-item label="一级学科" prop="fistSubject">
                <el-input v-model="form.fistSubject" style="width: 300px" placeholder="请输入一级学科"></el-input>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item label="发证机关" prop="licenceissuingAuthority">
                <el-input v-model="form.licenceissuingAuthority" style="width: 400px" placeholder="请输入发证机关"></el-input>
              </el-form-item>
              <el-form-item label="获奖级别" prop="awardLevel">
                <el-select v-model="form.awardLevel" placeholder="请选择获奖级别">
                  <el-option
                    v-for="dict in dict.type.work_award_level"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="获奖奖种">
                <el-input v-model="form.pizeSpecies" style="width: 400px" placeholder="请输入获奖奖种"></el-input>
              </el-form-item>
              <el-form-item label="是否论文征集" prop="isCallForPapers">
                <el-radio v-model="form.isCallForPapers" label="1">是</el-radio>
                <el-radio v-model="form.isCallForPapers" label="2">否</el-radio>
              </el-form-item>
              <el-form-item label="单位排名" prop="unitRanking">
                <el-select v-model="form.unitRanking" placeholder="请选择单位排名">
                  <el-option
                    v-for="dict in dict.type.unit_ranking"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="学科门类" prop="subjectCategoryId">
                <el-radio v-model="form.subjectCategoryId" label="1">社科类</el-radio>
                <el-radio v-model="form.subjectCategoryId" label="2">科技类</el-radio>
              </el-form-item>
              <el-form-item label="项目来源" prop="projectSource">
                <el-input v-model="form.projectSource" style="width: 400px" placeholder="请输入项目来源"></el-input>
              </el-form-item>
            </el-col>

          </el-row>
        </el-form>
      </div>
    </div>

    <div>
      <div class="head_2"><p>[获奖完成人信息]</p></div>
      <div>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddWorkAwardWinners">添加作者</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteWorkAwardWinners">删除作者</el-button>
          </el-col>
        </el-row>
        <el-table :data="awardWinnersList" :row-class-name="rowWorkAwardWinnersIndex" @selection-change="handleWorkAwardWinnersSelectionChange" ref="workAwardWinners">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="署名顺序" prop="signatureOrder" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.signatureOrder" placeholder="请输入署名顺序" />
            </template>
          </el-table-column>
          <el-table-column label="作者类型" prop="authorType" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.authorType" placeholder="请选择作者类型" @change="handleStu(scope.row)">
                <el-option
                  v-for="dict in dict.type.author_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="完成人" prop="parsonCharge" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.parsonCharge" placeholder="请输入完成人" />
            </template>
          </el-table-column>
          <el-table-column label="性别" prop="gender" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.gender" placeholder="请选择性别">
                <el-option
                  v-for="dict in dict.type.sys_user_sex"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="学历" prop="educationBackgorund" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.educationBackgorund" placeholder="请选择学历">
                <el-option
                  v-for="dict in dict.type.education_backgorund"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="工作单位" prop="affiliatedUnit" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.affiliatedUnit" placeholder="请输入工作单位" />
            </template>
          </el-table-column>
          <el-table-column label="贡献率" prop="contributionRate" width="150">
            <template slot="header" scope="scope">贡献率 (%)</template> <!-- 表头显示带百分号 -->
            <template slot-scope="scope">
              <el-input
                v-model="scope.row.contributionRate"
                placeholder="请输入贡献率"
                class="contribution-input"
                @input="handleInput(scope.row)"
              /><span class="percent-sign">%</span>
            </template>
          </el-table-column>
          <el-table-column label="职称" prop="professionalTitle" width="150" >
            <template slot-scope="scope">
              <el-select v-model="scope.row.professionalTitle" placeholder="请选择职称" :disabled="isStu">
                <el-option :disabled="isStu"
                  v-for="dict in dict.type.professional_title"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>

    <div>
      <div class="head_2"><p>[附件上传]</p></div>
      <div>
        <el-form :model="form1" label-width="120px" :rules="rules" ref="form1">
          <el-form-item label="附件信息" prop="filesUrl">
            <file-upload v-model="form.filesUrl"/>
          </el-form-item>
        </el-form>
      </div>
    </div>

    <div>
      <el-button type="success" @click="handleSubmit" style="margin-left: 70%">提交</el-button>
    </div>
  </div>
</template>
<script>
import { listAward, getAward, delAward, addAward, updateAward } from "@/api/work/award";
import { listAwardWinners, getAwardWinners, delAwardWinners, addAwardWinners, updateAwardWinners } from "@/api/work/awardWinners";

export default {
  name: "Award",
  dicts: ['author_type', 'unit_ranking', 'work_award_grade', 'work_award_level', 'sys_yes_no', 'sys_user_sex', 'subject_category', 'professional_title','education_backgorund',],
  data() {
    return {
      isStu:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedWorkAwardWinners: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 获奖表格数据
      awardList: [],
      // 获奖完成人表格数据
      awardWinnersList: [],
      // form1: {
      //   pageNum: 1,
      //   pageSize: 10,
      //   workAwardId: null,
      //   signatureOrder: null,
      //   authorType: null,
      //   parsonCharge: null,
      //   gender: null,
      //   educationBackgorund: null,
      //   affiliatedUnit: null,
      //   contributionRate: null,
      //   professionalTitle: null
      // },
      // 弹出层标题
      title: "",
      // 查询参数
      form: {
        pageNum: 1,
        pageSize: 10,
        awardName: null,
        awardDate: null,
        awardGrade: null,
        fistWinner: null,
        resultName: null,
        winnersNumber: null,
        licenceissuingAuthority: null,
        awardLevel: null,
        pizeSpecies: null,
        completionUnit: null,
        affiliatedUnit: null,
        isCallForPapers: null,
        participationgUnitsNum: null,
        unitRanking: null,
        awardCode: null,
        subjectCategoryId: null,
        fistSubject: null,
        projectSource: null,
        resultForm: null,
        filesUrl: null,
        awardType: null,
        dataAddUserId: null,
        dataAddDate: null,
        dataSchoolAuditUserId: null,
        dataSchoolAuditDate: null,
        dataSchoolAuditStatus: null,
        dataFacultyAuditUserId: null,
        dataFacultyAuditDate: null,
        dataFacultyAuditStatus: null
      },
      form1:{
        filesUrl: null,
      },
      // 表单校验
      rules: {
        awardName: [
          { required: true, message: '奖励名称不能为空', trigger: 'blur' }
        ],
        resultName: [
          { required: true, message: '成果名称不能为空', trigger: 'blur' }
        ],
        affiliatedUnit: [
          { required: true, message: '所属单位不能为空', trigger: 'blur' }
        ],
        winnersNumber: [
          { required: true, message: '获奖人数不能为空', trigger: 'blur' },
          { type: 'number', message: '获奖人数必须为数字' }
        ],
        winningDate: [
          { type: 'date', message: '获奖日期格式不正确', trigger: 'change' }
        ],
        awardLevel: [
          { required: true, message: '获奖级别不能为空', trigger: 'change' }
        ],
        completionUnit: [
          { required: true, message: '完成单位不能为空', trigger: 'blur' }
        ],
        participationgUnitsNum: [
          { required: true, message: '总参加单位数不能为空', trigger: 'blur' },
          { type: 'number', message: '总参加单位数必须为数字' }
        ],
        awardCode: [
          { required: true, message: '奖励批准号不能为空', trigger: 'blur' }
        ],
        fistSubject: [
          { required: true, message: '一级学科不能为空', trigger: 'blur' }
        ],
        licenceissuingAuthority: [
          { required: true, message: '发证机关不能为空', trigger: 'blur' }
        ],
        prizeSpecies: [
          { required: true, message: '获奖奖种不能为空', trigger: 'blur' }
        ],
        isCallForPapers: [
          { required: true, message: '是否论文征集不能为空', trigger: 'blur' }
        ],
        unitRanking: [
          { required: true, message: '单位排名不能为空', trigger: 'blur' }
        ],
        subjectCategoryId: [
          { required: true, message: '学科门类不能为空', trigger: 'blur' }
        ],
        projectSource: [
          { required: true, message: '项目来源不能为空', trigger: 'blur' }
        ],
        filesUrl: [
          { required: true, message: '请选取相应文件', trigger: 'blur' }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    next() {
      if (this.active++ > 2) this.active = 0;
    },
    /** 查询获奖列表 */
    async getList() {
      this.loading = true;
      // let response = await listAward(this.queryParams)
      //   this.awardList = response.rows;
      //  this.awardWinnersList = (await listAwardWinners())
    },
    /** 获奖完成人序号 */
    rowWorkAwardWinnersIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 获奖完成人添加按钮操作 */
    handleAddWorkAwardWinners() {
      let obj = {};
      obj.signatureOrder = "";
      obj.authorType = "";
      obj.parsonCharge = "";
      obj.gender = "";
      obj.educationBackgorund = "";
      obj.affiliatedUnit = "";
      obj.contributionRate = "";
      obj.professionalTitle = "";
      this.awardWinnersList.push(obj);
    },
    /** 获奖完成人删除按钮操作 */
    handleDeleteWorkAwardWinners() {
      if (this.checkedWorkAwardWinners.length == 0) {
        this.$modal.msgError("请先选择要删除的获奖完成人数据");
      } else {
        const awardWinnersList = this.awardWinnersList;
        const checkedWorkAwardWinners = this.checkedWorkAwardWinners;
        this.awardWinnersList = awardWinnersList.filter(function(item) {
          return checkedWorkAwardWinners.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleWorkAwardWinnersSelectionChange(selection) {
      this.checkedWorkAwardWinners = selection.map(item => item.index)
    },
    /** 贡献率判断 */
    handleInput(row) {
      const value = row.contributionRate;
      if (value < 0 || value > 100) {
        this.$message.error('贡献率必须在0%到100%之间');
        row.contributionRate = '';
      }
    },
    /** 完成人是学生的判断 */
    handleStu(row){
      if(row.authorType=='1'){
        this.isStu = true;
        row.professionalTitle = '';
      }
      if(row.authorType=='2'){
        this.isStu = false;
        row.professionalTitle = '';
      }
    },
    /**提交*/
    handleSubmit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.awardWinnersList.length === 0) {
            this.$message.error('请添加完成人信息');
            return;
          }
          this.$refs.form1.validate((valid1) => {
            if (valid1) {
              this.form.filesUrl = this.form1.filesUrl;
              // addAward(this.form);
              // this.awardWinnersList[0].workAwardId = this.form.id;
              // addAwardWinners(this.awardWinnersList[0])

            }
          });
        }
      });
    },
  }
};
</script>
<style lang="scss">
.head {
  position: relative;
  height:40px;
  background-color: #1ab394;
  border-radius: 10px;
  margin-top: 20px;
  margin-bottom: 20px;
  display: flex;
  align-items: center;



}
p{
  margin-left: 20px;
  color: #371ce3;
  font-weight: 600;
}
.head_2 {
  height:40px;
  background: linear-gradient(to right, rgba(162, 252, 230, 0.98), #ffffff);
  border-radius: 10px;
  margin-top: 20px;
  margin-bottom: 20px;
  display: flex;
  align-items: center;

}
.contribution-input {
  display: inline-block;
  width: calc(100% - 20px); /* 减去百分号占据的空间和一些边距 */
}

.percent-sign {
  display: inline-block;
  width: 20px; /* 百分号占据的空间 */
  text-align: center;
}
</style>
